/**
 * Created by zhaojm on 15/3/21.
 */
game.HubLayer = cc.Layer.extend({

    _operate_left_sprite : null,
    _operate_right_sprite : null,

    _operate_top_menu_item : null,

    _menu : null,

    _touchListener : null,

    _touchEnabled : null,

    _guideBtn : null,

    ctor:function(){
        this._super();
        var size = cc.winSize;
        game._Globals.hubLayer = this;
        this._touchEnabled = true;
        operateFrame = cc.spriteFrameCache.getSpriteFrame('operate.png');

        this._operate_left_sprite = new cc.Sprite(operateFrame);
        this._operate_left_sprite.setPosition(cc.p(size.width * 0.3, 10));
        this._operate_left_sprite.setAnchorPoint(cc.p(1, 0));
        this.addChild(this._operate_left_sprite);


        this._operate_right_sprite = new cc.Sprite(operateFrame);
        this._operate_right_sprite.setPosition(cc.p(size.width * 0.7, 10));
        this._operate_right_sprite.setAnchorPoint(cc.p(1, 1));
        this._operate_right_sprite.setRotation(180);
        this.addChild(this._operate_right_sprite);

        var self = this;
        this._operate_top_menu_item = new cc.MenuItemImage(operateFrame, operateFrame, operateFrame,  function(){
            //cc.log('onTopClicked');
            if(self._touchEnabled == false){
                return;
            }
            if(self.parent._isGuide ){
                if(self._guideBtn == 2){
                    self.parent._uiLayer.onGuideButtonCallback();
                    game._Globals.hero.jump();
                }
            }else{
                game._Globals.hero.jump();
            }


        }, this);

        this._operate_top_menu_item.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.1 + 8));
        this._operate_top_menu_item.setRotation(90);
        this._operate_top_menu_item.setScale(1.3);
        this._operate_top_menu_item.setAnchorPoint(cc.p(1, 0.5));

        this._menu = new cc.Menu(this._operate_top_menu_item);
        this._menu.setAnchorPoint(cc.p(0, 0));
        this._menu.setPosition(cc.p(0, 0));

        this.addChild(this._menu);



        var listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true   ,       // true 为不向下传递
            onTouchBegan: function(touch, event){
                if(self._touchEnabled == false){
                    return;
                }
                var touchPos = touch.getLocation();
                var size = cc.winSize;

                if(touchPos.x >= size.width * 0.5){
                    game._Globals.hubLayer.onRightClicked();
                }else{
                    game._Globals.hubLayer.onLeftClicked();
                }
            }
            //onTouchMoved: this.onTouchMoved,
            //onTouchEnded: this.onTouchEnded,
            //onTouchCancel: this.onTouchCancel
        });
        this._touchListener = listener;
        cc.eventManager.addListener(listener, this);

    },





    onLeftClicked : function(){
        //cc.log('onLeftClicked');
        var self = game._Globals.hubLayer;

        if(self.parent._isGuide ){
            if(self._guideBtn == 0){
                self.parent._uiLayer.onGuideButtonCallback();
                game._Globals.hero.moveLeft();
            }
        }else{
            game._Globals.hero.moveLeft();
        }
        //game._Globals.hero.moveLeft();
    },
    onRightClicked : function(){
        //cc.log('onRightClicked');
        var self = game._Globals.hubLayer;
        if(self.parent._isGuide ){
            if(self._guideBtn == 1){
                self.parent._uiLayer.onGuideButtonCallback();
                game._Globals.hero.moveRight();
            }
        }else{
            game._Globals.hero.moveRight();
        }
        //game._Globals.hero.moveRight();
    },

    setTouchEnabled:function(b){
        this._touchEnabled  = b;
    },

    setGuideButton:function(num){
        this._guideBtn = num;
    },



});
